﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Negocios;

namespace MiraPark
{
    public partial class FormCabineFinanceiro : Form
    {
        public FormCabineFinanceiro()
        {
            InitializeComponent();
        }
        private void FormCabineFinanceiro_Load(object sender, EventArgs e)
        {
            List<PatioVO> listPatioVOs = ModelBO.getPatios();
            listPatioVOs.Add(PatioVO.CreatePatioVO(-1,""));
            this.comboBoxPatio.DataSource=listPatioVOs;
            this.comboBoxPatio.SelectedValue=-1;
            this.dateTimePickerDataMov.Value = DateTime.Today;


        }

        private void comboBoxPatio_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxPatio.SelectedItem != null)
            {
                PatioVO patioVO = (PatioVO)this.comboBoxPatio.SelectedItem;
                if (patioVO.idPatio != -1)
                {
                    List<CabineVO> listCabineVOs = ModelBO.getCabines(null, patioVO.idPatio);
                    listCabineVOs.Add(CabineVO.CreateCabineVO(-1, "", patioVO.idPatio));
                    this.comboBoxCabine.DataSource = listCabineVOs;
                    this.comboBoxCabine.SelectedValue = -1;
                }
                else
                {
                    this.comboBoxCabine.DataSource = null;
                }
            }

        }

        private void comboBoxCabine_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxCabine.SelectedItem != null)
            {
                CabineVO cabineVO = (CabineVO)this.comboBoxCabine.SelectedItem;
                if (cabineVO.idCabine != -1)
                {
                    this.dataGridViewMovimentos.DataSource = ModelBO.getCabineXMovimento(cabineVO.idCabine, this.dateTimePickerDataMov.Value, 30);
                }
                else
                {
                    this.dataGridViewMovimentos.DataSource = null;
                }
            }
        }

        private void dateTimePickerDataMov_ValueChanged(object sender, EventArgs e)
        {
            comboBoxCabine_SelectedIndexChanged(sender, e);
        }

        private void buttonLancar_Click(object sender, EventArgs e)
        {
            
            StringBuilder mensagem = new StringBuilder();

            if (this.comboBoxCabine.SelectedItem == null || (int)this.comboBoxCabine.SelectedValue == -1)
            {
                mensagem.AppendLine("Cabine deve ser informada.");
            }
            if (this.textBoxMovimento.Text.Trim().Length == 0)
            {
                mensagem.AppendLine("Descrição do movimento deve ser informada.");
            }
            if (this.numericUpDownCredito.Value + this.numericUpDownDebito.Value == 0)
            {
                mensagem.AppendLine("Crédito ou Débito devem ser informados.");
            }
            if (mensagem.ToString().Length > 0)
            {
                MessageBox.Show(mensagem.ToString(), "Aviso");
                return;
            }

            DialogResult dr = MessageBox.Show(this, "Confirma o lançamento de movimento?", "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString().Equals("No"))
            {
                return;
            }

            decimal saldo = ModelBO.getCabineSaldo((int)this.comboBoxCabine.SelectedValue);
            
            CabineXMovimentoVO cabineXMovimentoVO = CabineXMovimentoVO.CreateCabineXMovimentoVO();
            cabineXMovimentoVO.idCabine = (int)this.comboBoxCabine.SelectedValue;
            cabineXMovimentoVO.dataMov = DateTime.Now;
            cabineXMovimentoVO.movimento = this.textBoxMovimento.Text;
            cabineXMovimentoVO.credito = this.numericUpDownCredito.Value;
            cabineXMovimentoVO.debito = this.numericUpDownDebito.Value;
            cabineXMovimentoVO.saldo = saldo + this.numericUpDownCredito.Value - this.numericUpDownDebito.Value;

            ModelBO.newCabineXMovimento(cabineXMovimentoVO);
            comboBoxCabine_SelectedIndexChanged(sender, e);

        }

    }
}
